<?php

namespace app\api\controller\product;


use app\api\controller\Api;
use app\common\product\model\ProductRuleValue;

class Spec extends Api
{


    public function create()
    {
        $post = $this->request->post();
        $id = $post['id'];
        // $type = $post['type'];
        // 获取当前所有规格值
        $ruleValueModel = new ProductRuleValue();
        $ruleValueMap = $ruleValueModel->where('rule_id', $id)->column('id,parent_id,value', 'value');

        $attr = $post['attrs'];
        $details = attr_format($attr)[1];
        $attrData = [];
        $count = 0;
        // $detailMap = $post['details'];
        foreach ($details as $key => $item) {
            $detail = $item['detail'];
            $map = [];
            foreach ($detail as $v => $d) {
                $d = trim($d);
                $detail[$v] = $d;
                // 查找规格值的id及parent_id
                $pid = $ruleValueMap[$d]['parent_id'];
                $map[$pid] = $ruleValueMap[$d]['id'];
            }

            $suk = implode(',', $detail);
            $sukValue[$suk]['pic'] = '';
            $sukValue[$suk]['price'] = 0;
            $sukValue[$suk]['cost'] = 0;
            $sukValue[$suk]['ot_price'] = 0;
            $sukValue[$suk]['stock'] = 0;
            $sukValue[$suk]['bar_code'] = '';
            $sukValue[$suk]['weight'] = 0;
            $sukValue[$suk]['volume'] = 0;
            $sukValue[$suk]['brokerage'] = 0;
            $sukValue[$suk]['brokerage_two'] = 0;

            //编辑商品时，将没有规格的数据不生成默认值
            /**
            foreach (array_keys($detail) as $k => $title) {
                $header[$k]['title'] = $title;
                $header[$k]['align'] = 'center';
                $header[$k]['minWidth'] = 130;
            }
            **/
            $index = 0;
            foreach ($detail as $k => $v) {
                $attrData[$count]['value' . ($index + 1)] = $v;
                $index++;
            }

            $attrData[$count]['suk_ids'] = $map;
            $attrData[$count]['detail'] = $detail;
            $attrData[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
            $attrData[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
            $attrData[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
            $attrData[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
            $attrData[$count]['vip_price'] = isset($sukValue[$suk]['vip_price']) ? floatval($sukValue[$suk]['vip_price']) : 0;
            $attrData[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
            $attrData[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
            $attrData[$count]['weight'] = floatval($sukValue[$suk]['weight']) ?? 0;
            $attrData[$count]['volume'] = floatval($sukValue[$suk]['volume']) ?? 0;
            $attrData[$count]['brokerage'] = floatval($sukValue[$suk]['brokerage']) ?? 0;
            $attrData[$count]['brokerage_two'] = floatval($sukValue[$suk]['brokerage_two']) ?? 0;
            $count++;

        }

        $result = [];
        $result['attrData'] = $attrData;
        $result['attrs'] = array_keys($details[0]['detail']);

        return success($result);
    }
}